import java.util.ArrayList;
import java.util.List;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param pRoot TreeNode类
     * @return bool布尔型
     */
    public boolean IsBalanced_Solution (TreeNode pRoot) {

        if(pRoot==null){
            return true;
        }
        List<TreeNode> list=pre(pRoot);
        for(TreeNode node:list){

            int left=heightOf(node.left);
            int right=heightOf(node.right);
            int diff=left-right;
            if(diff>1||diff<-1){
                return false;
            }
        }
        return true;
    }
    public int heightOf(TreeNode pRoot){
        if(pRoot==null){
            return 0;
        }
        return 1+Math.max(heightOf(pRoot.left),heightOf(pRoot.right));

    }
    public List<TreeNode> pre(TreeNode pRoot){
        List<TreeNode> list=new ArrayList<>();
        if(pRoot==null){

            return list;
        }
        list.add(pRoot);
        list.addAll(pre(pRoot.left));
        list.addAll(pre(pRoot.right));
        return list;
    }
}